using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodeStory.WebControls
{
	public class StateTemplate : IStateManager
	{
		StateBag _viewState;

		protected StateBag ViewState {
			get {
				if (_viewState == null) {
					_viewState = new StateBag();
					if (IsTrackingViewState) {
						((IStateManager)_viewState).TrackViewState();
					}
				}
				return _viewState;
			}
		}

		[NotifyParentProperty(true)]
		[DefaultValue("")]
		public string State {
			get {
				object obj = ViewState["State"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["State"] = value; }
		}

		[NotifyParentProperty(true)]
		[DefaultValue("")]
		public string ImageUrl {
			get {
				object obj = ViewState["ImageUrl"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["ImageUrl"] = value; }
		}

		[NotifyParentProperty(true)]
		[DefaultValue(BorderStyle.None)]
		public BorderStyle BorderStyle {
			get {
				object obj = ViewState["BorderStyle"];
				return obj == null ? BorderStyle.None : (BorderStyle)obj;
			}
			set { ViewState["BorderStyle"] = value; }
		}

		[NotifyParentProperty(true)]
		[DefaultValue(typeof(Color),"")]
		[TypeConverter(typeof(WebColorConverter))]
		public Color BorderColor {
			get { 
				object obj = ViewState["BorderColor"];
				return obj == null ? Color.Empty : (Color)obj;
			}
			set { ViewState["BorderColor"] = value; }
		}

		[DefaultValue(typeof(Unit), "")]
		public Unit BorderWidth {
			get { 
				object obj = ViewState["BorderWidth"];
				return obj == null ? Unit.Empty : (Unit)obj;
			}
			set { ViewState["BorderWidth"] = value; }
		}


		#region IStateManager Members

		bool _isTrackingViewState = false;
		public bool IsTrackingViewState {
			get { return _isTrackingViewState; }
		}

		public void LoadViewState(object state) {
			if (state != null) {
				((IStateManager)ViewState).LoadViewState(state);
			}
		}

		public object SaveViewState() {
			if (_viewState != null) {
				return ((IStateManager)_viewState).SaveViewState();
			}
			return null;
		}

		public void TrackViewState() {
			_isTrackingViewState = true;
		}

		bool IStateManager.IsTrackingViewState {
			get { return IsTrackingViewState; }
		}

		void IStateManager.LoadViewState(object state) {
			LoadViewState(state);
		}

		object IStateManager.SaveViewState() {
			return SaveViewState();
		}

		void IStateManager.TrackViewState() {
			TrackViewState();
		}

		#endregion
	}
}